package com.yanguilai.rabbitmqtest.recv;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

@Component
public class Receive {

    private final static String QUEUE_NAME = "hello";

    private static ConnectionFactory factory;

    static {
        factory = new ConnectionFactory();
        factory.setHost("c7formal");
    }

    public void receive() {
//        try {
//            Connection connection = factory.newConnection();
//            Channel channel = connection.createChannel();
//
//            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
//
//            DeliverCallback callback = (tag, delivery) -> {
//                String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
//                System.out.println(" [x] Received '" + msg + "'");
//            };
//            channel.basicConsume(QUEUE_NAME, true, callback, tag -> {});
//        } catch (TimeoutException | IOException e) {
//            e.printStackTrace();
//        }
    }
}
